package cn.echase.ecare.yinfayun.service.impl;

import cn.echase.common.core.enums.ResultCode;
import cn.echase.ecare.yinfayun.domain.dto.ElderStatisticsDto;
import cn.echase.ecare.yinfayun.domain.dto.StatisticsDto;
import cn.echase.ecare.yinfayun.domain.entity.Building;
import cn.echase.ecare.yinfayun.domain.mapper.OrgBaseInfoStatisticsMapper;
import cn.echase.ecare.yinfayun.domain.vo.CareItemVo;
import cn.echase.ecare.yinfayun.domain.vo.CareLevelStatisticVo;
import cn.echase.ecare.yinfayun.domain.vo.OrgElderCenterVo;
import cn.echase.ecare.yinfayun.service.ICareLevelStatisticService;
import cn.echase.ecare.yinfayun.service.ToolService;
import cn.echase.ecare.yinfayun.utils.PageUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 照护等级数据统计Service业务层处理
 *
 * @author yinyy
 * @date 2024-11-07
 */
@Service
@Slf4j
public class CareLevelStatisticServiceImpl implements ICareLevelStatisticService {

    @Autowired
    private ToolService toolService;

    @Resource
    private OrgBaseInfoStatisticsMapper statisticsMapper;

    /**
     * 查询机构照护等级数据统计分页列表
     *
     * @param statisticsDto 查询条件
     * @return 机构照护等级数据统计列表
     */
    @Override
    public TableDataInfo<CareLevelStatisticVo> careLevelStatisticPageList(StatisticsDto statisticsDto) {
        List<CareLevelStatisticVo> resultList = this.careLevelStatisticList(statisticsDto);
        return PageUtil.getResultTableDataInfoList(resultList, statisticsDto.getPageNum(), statisticsDto.getPageSize());
    }


    @Override
    public List<CareLevelStatisticVo> careLevelStatisticList(StatisticsDto statisticsDto) {
        List<CareLevelStatisticVo> resultList = new ArrayList<>();
        List<CareItemVo> eldCareLevelList = statisticsMapper.getBuildingCareLevelList(statisticsDto);
        List<CareItemVo> orgCareLevelList = statisticsMapper.getOrgCareLevelInfo(statisticsDto);

        Building buildingQuery = new Building();
        buildingQuery.setOrgId(statisticsDto.getOrgId());
        List<Building> buildingList = toolService.getList(buildingQuery);
        if (buildingList == null || buildingList.isEmpty()) {
            return resultList;
        }

        if (eldCareLevelList == null || eldCareLevelList.isEmpty()) {
            return resultList;
        }

        if (orgCareLevelList == null || orgCareLevelList.isEmpty()) {
            return resultList;
        }

        //楼层，照护等级，人数
        Map<String, Map<String, String>> buildingMap = new HashMap<>();
        for (CareItemVo vo : eldCareLevelList) {
            if (buildingMap.get(vo.getBuildingId()) == null) {
                Map<String, String> careMap = new HashMap<>();
                careMap.put(vo.getCareId(), vo.getPersonCount());
                buildingMap.put(vo.getBuildingId(), careMap);
            } else {
                Map<String, String> careMap = buildingMap.get(vo.getBuildingId());
                careMap.put(vo.getCareId(), vo.getPersonCount());
            }
        }


        for (Building building : buildingList) {
            CareLevelStatisticVo rs = new CareLevelStatisticVo();
            rs.setBuildingId(building.getUcId());
            rs.setBuildingName(building.getBuildingName());
            int sum = 0;
            Map<String, Integer> careLevelCount = new HashMap<>();
            Map<String, String> careMap = buildingMap.get(building.getUcId());
            for (CareItemVo orgVo : orgCareLevelList) {
                if (careMap != null && careMap.get(orgVo.getCareId()) != null) {
                    int peopleCount = Integer.parseInt(careMap.get(orgVo.getCareId()));
                    careLevelCount.put(orgVo.getCareId(), peopleCount);
                    sum += peopleCount;
                } else {
                    careLevelCount.put(orgVo.getCareId(), 0);
                }
            }
            rs.setCareLevelCount(careLevelCount);
            rs.setSumCount(sum);
            resultList.add(rs);
        }

        return resultList;
    }


    /**
     * 照护等级/楼栋相应长者列表查询
     *
     * @param elderStatisticsDto 查询条件
     * @return 长者列表
     */
    @Override
    public TableDataInfo<OrgElderCenterVo> getCareLevelElderList(ElderStatisticsDto elderStatisticsDto) {
        PageHelper.startPage(elderStatisticsDto.getPageNum(), elderStatisticsDto.getPageSize());
        List<OrgElderCenterVo> pageList = statisticsMapper.getCareLevelElderList(elderStatisticsDto);

        if (CollUtil.isEmpty(pageList)) {
//            return TableDataInfo.build(ResultCode.SUCCESS.getCode(), pageList);

            return null; // TODO 需做兼容
        }else {
            boolean singleBuilding = toolService.isSingleBuilding();
            if (singleBuilding) {
                for (OrgElderCenterVo vo : pageList) {
                    String splitBedName = toolService.splitBedName(vo.getBedName(), null);
                    vo.setBedName(splitBedName);
                }
            }
        }

//        return TableDataInfo.build(ResultCode.SUCCESS.getCode(), pageList);

        return null; // TODO 需做兼容
    }

}
